www.gusucode.com > VC++ RC4文件加密解密的实现-源码程序 > VC++ RC4文件加密解密的实现-源码程序/code/RC4_CPPDlg.cpp

    // RC4_CPPDlg.cpp : implementation file
// Downlaod by http://www.NewXing.com

#include "stdafx.h"
#include "RC4_CPP.h"
#include "RC4_CPPDlg.h"

#include "rc4.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRC4_CPPDlg dialog

CRC4_CPPDlg::CRC4_CPPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRC4_CPPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRC4_CPPDlg)
	m_strOpenFile = _T("");
	m_strSaveFile = _T("");
	m_strKey = _T("");
	m_strTime = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRC4_CPPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRC4_CPPDlg)
	DDX_Text(pDX, IDC_EDIT_FILE_NAME, m_strOpenFile);
	DDX_Text(pDX, IDC_EDIT_FILE_SAVE, m_strSaveFile);
	DDX_Text(pDX, IDC_EDIT_KEY, m_strKey);
	DDX_Text(pDX, IDC_EDIT_TIME, m_strTime);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRC4_CPPDlg, CDialog)
	//{{AFX_MSG_MAP(CRC4_CPPDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_ENCODE, OnButtonEncode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRC4_CPPDlg message handlers

BOOL CRC4_CPPDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRC4_CPPDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRC4_CPPDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CRC4_CPPDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
	if(IDOK!=dlg.DoModal())return;
	m_strOpenFile=dlg.GetPathName();	
	UpdateData(FALSE);
}

void CRC4_CPPDlg::OnButtonSave() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
	if(IDOK!=dlg.DoModal())return;
	m_strSaveFile=dlg.GetPathName();	
	UpdateData(FALSE);
}

void CRC4_CPPDlg::OnButtonEncode() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFile fSou(m_strOpenFile,CFile::modeRead);
	if (m_strSaveFile=="")
		m_strSaveFile=m_strOpenFile+".RC4";
	CFile fDes(m_strSaveFile,CFile::modeCreate|CFile::modeWrite);
	fSou.SeekToBegin();
	fDes.SeekToBegin();

	CTime tmStart,tmEnd;
	tmStart=CTime::GetCurrentTime();

	rc4 rc4_cona((unsigned char *)m_strKey.GetBuffer(0),m_strKey.GetLength());
	char *buf[BUF_SIZE];
	UINT uLen = fSou.Read(buf,BUF_SIZE);
	while (uLen>0)
	{
		rc4_cona.rc4_encode((unsigned char*)buf,uLen);
		//注意,每进行一次加密,key就会变换,紧接着使用rc4_encode解密就不正确
		fDes.Write(buf,uLen);
		uLen = fSou.Read(buf,BUF_SIZE);
	}

	fSou.Close();
	fDes.Close();

	tmEnd=CTime::GetCurrentTime();
	CTimeSpan tmSpan=tmEnd-tmStart;
	CString strspan=tmSpan.Format("Seconds:%S");
	m_strTime=strspan+"s";

	AfxMessageBox("完成!");
	UpdateData(FALSE);
}